import { Component, Injector, Input, SimpleChange } from '@angular/core';
import { NavigationExtras, Router } from '@angular/router';
import { AppComponentBase } from '@shared/common/app-component-base';
import { AppMenuService } from '@app/shared/common/menu/app-menu.service';
import { BreadcrumbItem } from '@app/shared/layout/nav/breadcrumb-item';
import { AppNavigationService } from '@app/shared/layout/nav/app-navigation.service';

@Component({
    selector: 'sub-header',
    templateUrl: './sub-header.component.html'
})
export class SubHeaderComponent extends AppComponentBase {

    @Input() title: string;
    @Input() description: string;
    @Input() kind: number;
    @Input() breadcrumbs: BreadcrumbItem[];
    @Input() idName: string;

    constructor(
        private _appMenuService: AppMenuService,
        private _appNavService: AppNavigationService,
        private _router: Router,
        injector: Injector
    ) {
        super(injector);
    }

    ngOnChanges(changes: SimpleChange) {
        if (changes['kind']) {
            if (this.kind) {
                //this.title = this._appMenuService.getMenuName(this.kind);
                this.breadcrumbs = this._appMenuService.getBreadcrumbsForMenu(this.kind);
            }
            
        }

        if (changes['idName']) {
            if (this.idName) {
                this.breadcrumbs = this._appNavService.getBreadcrumbsForSystemMenu(this.idName);
            }
        }
    }

    goToBreadcrumb(breadcrumb: BreadcrumbItem): void {
        if (!breadcrumb || !breadcrumb.routerLink) {
            return;
        }

        if (breadcrumb.navigationExtras) {
            this._router.navigate([breadcrumb.routerLink], breadcrumb.navigationExtras)
                .then(() => { this._router.onSameUrlNavigation = 'reload' });
        } else {
            this._router.navigate([breadcrumb.routerLink])
                .then(() => { this._router.onSameUrlNavigation = 'reload' });
        }
    }
}
